$(function () {
    //显示所有数据
    $.ajax({
        url: "/hospital/transfer/all.action",
        type: "GET",
        dataType: "json",
        success: function (data) {
            var $list = $("#tb_turnHospital");
            $.each(data, function (index, ele) {
                var $item = createDataItem(index, ele);
                $list.append($item);
            });
        },
        error: function () {
            alert("获取数据失败");
        }
    });

    //1.1 动态创建一条数据
    function createDataItem(index, data) {
        var $item = $("" + "<tr>\n" +
            "            <td style=\"width:100px;\">" + (index + 1) + "</td>\n" +
            "            <td>" + data.patName + "</td>\n" +
            "            <td>" + data.patSex + "</td>\n" +
            "            <td>" + data.patAge + "</td>\n" +
            "            <td>" + data.patContactInfo + "</td>\n" +
            "            <td>" + data.transTowhere + "</td>\n" +
            "            <td>" + data.transTime + "</td>\n" +
            "            <td>" + data.transPhysician + "</td>\n" +
            "            <td>" + data.transRemark + "</td>\n" +
            "            <td><button class=\"turnHospitalInformationShowUpdateBtn\" data-id=\"" + data.id + "\">编辑</button><button class=\"turnHospitalInformationShowCancelBtn\" data-id=\"" + data.id + "\">删除</button></td>\n" +
            "        </tr>");
        return $item;
    }

    //操作界面新增按钮点击事件*********************************
    $("#turnHospitalOperInputAddBtn").click(function () {
        // alert("新增按钮点击事件");
        $(".turnHospitalAddInformation").css("display", "block");
        $(".turnHospitalInformationShow").css("display", "none");
    });
    //按姓名查询按钮点击事件***********************************
    $("#turnHospitalOperSelectNameBtn").click(function () {
        // alert("按照姓名查询");
        var value = $("#turnHospitalOperInputName").val();
        $.ajax({
            url: "/hospital/transfer/patName.action",
            type: "POST",
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            data: JSON.stringify({"patName": value}),
            success: function (data) {
                var $list = $("#tb_turnHospital");
                //1.清空表格数据
                $("#tb_turnHospital tr:not(:first)").html("");
                //2.动态创建数据
                $.each(data, function (index, ele) {
                    var $item1 = createDataItem(index, ele);
                    $list.append($item1);
                });
            },
            error: function () {
                alert("查询失败");
            }
        })
    });
    //按日期查询点击事件*************************************
    // $("#turnHospitalOperSelectDateBTn").click(function () {
    //    alert("按照日期查询");
    //    var value=$("#turnHospitalOperInputDate").val();
    //    $.ajax({
    //        url:"/hospital/transfer/transTime.action",
    //        type:"POST",
    //        contentType:"application/json;charset=utf-8",
    //        dataType:JSON,
    //        data:JSON.stringify({"transTime":value}),
    //        success:function (data) {
    //            var $list = $("#tb_turnHospital");
    //            //1.清空表格数据
    //            $("#tb_turnHospital tr:not(:first)").html("");
    //            //2.动态创建数据
    //            $.each(data, function (index, ele) {
    //                var $item1 = createDataItem(index, ele);
    //                $list.append($item1);
    //            });
    //        },
    //        error:function () {
    //            alert("失败");
    //        }
    //    })
    // });
    $("#turnHospitalOperSelectDateBTn").click(function () {
        // alert("按照姓名查询");
        var value = $("#turnHospitalOperInputDate").val();
        $.ajax({
            url: "/hospital/transfer/transTime.action",
            type: "POST",
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            data: JSON.stringify({"transTime": value}),
            success: function (data) {
                var $list = $("#tb_turnHospital");
                //1.清空表格数据
                $("#tb_turnHospital tr:not(:first)").html("");
                //2.动态创建数据
                $.each(data, function (index, ele) {
                    var $item1 = createDataItem(index, ele);
                    $list.append($item1);
                });
            },
            error: function () {
                alert("查询失败");
            }
        })
    });
    //编辑按钮事件委托**************************************
    $("#tb_turnHospital").delegate(".turnHospitalInformationShowUpdateBtn", "click", function () {
        // alert("编辑按钮事件委托点击事件");
        var id = $(this).attr("data-id");
        // alert(id);
        //获取修改前的数据
        var value = $(this).parents("tr")[0];
        var value1 = value.children[1].innerHTML;
        var value2 = value.children[5].innerHTML;
        var value3 = value.children[6].innerHTML;
        var value4 = value.children[7].innerHTML;
        var value5 = value.children[8].innerHTML;
        $("#turnHospitalName").val(value1);
        $("#turnHospitalPlace").val(value2);
        $("#turnHospitalDate").val(value3);
        $("#turnHospitalPerson").val(value4);
        $("#turnHospitalRemark").val(value5);
        //显示编辑界面
        $(".turnHospitalClickUpdateGetInfo").css("display", "block");
        $("#over").css("display", "block");

        //编辑框确认按钮点击事件*******************************
        $("#turnHospitalclickUpdateGetInfoSureBtn").unbind("click").click(function () {
            //获取修改后的数据
            var where = $("#turnHospitalPlace").val();
            var time = $("#turnHospitalDate").val();
            var person = $("#turnHospitalPerson").val();
            var remark = $("#turnHospitalRemark").val();
            $.ajax({
                url: "/hospital/transfer/update.action",
                type: "POST",
                contentType: "application/json;charset=utf-8",
                dataType: "json",
                data: JSON.stringify({
                    "id": id,
                    "transTowhere": where,
                    "transTime": time,
                    "transPhysician": person,
                    "transRemark": remark
                }),
                success: function (data) {
                    var $list = $("#tb_turnHospital");
                    //1.清空表格数据
                    $("#tb_turnHospital tr:not(:first)").html("");
                    //2.动态创建数据
                    $.each(data, function (index, ele) {
                        var $item1 = createDataItem(index, ele);
                        $list.append($item1);
                    });
                    alert("修改成功");
                    $(".turnHospitalClickUpdateGetInfo").css("display", "none");
                    $("#over").css("display", "none");
                },
                error: function () {
                    alert("修改失败");
                }
            });
        });
        //编辑框取消按钮点击事件
        $("#turnHospitalclickUpdateGetInfoBackBtn").click(function () {
            $(".turnHospitalClickUpdateGetInfo").css("display", "none");
            $("#over").css("display", "none");
        });
    });
    //删除按钮事件委托*************************************
    $("#tb_turnHospital").delegate(".turnHospitalInformationShowCancelBtn", "click", function () {
        // alert("删除按钮事件委托点击事件");
        var id = $(this).attr("data-id");
        // alert(id);
        $.ajax({
            url: "/hospital/transfer/delete.action",
            type: "POST",
            dataType: "JSON",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify({"id": id}),
            success: function (data) {
                var $list = $("#tb_turnHospital");
                //1.清空表格数据
                $("#tb_turnHospital tr:not(:first)").html("");
                //2.动态创建数据
                $.each(data, function (index, ele) {
                    var $item1 = createDataItem(index, ele);
                    $list.append($item1);
                });
                alert("删除成功");
            },
            error: function () {
                alert("删除失败");
            }
        })
    });
    //新增信息编辑框确认按钮点击事件************************
    $("#turnHospitalAddSureBtn").click(function () {
        var patName = $("#turnHospitalAddInformationInputName").val();
        var where = $("#turnHospitalAddInformationInputPlace").val();
        var time = $("#turnHospitalAddInformationInputTime").val();
        var person = $("#turnHospitalAddInformationInputPerson").val();
        var remark = $("#turnHospitalAddInformationInputRemark").val();
        $.ajax({
            url: "/hospital/transfer/insert.action",
            type: "POST",
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            data: JSON.stringify({
                "patName": patName,
                "transTowhere": where,
                "transTime": time,
                "transPhysician": person,
                "transRemark": remark
            }),
            success: function (data) {
                var $list = $("#tb_turnHospital");
                //1.清空表格数据
                $("#tb_turnHospital tr:not(:first)").html("");
                //2.动态创建数据
                $.each(data, function (index, ele) {
                    var $item1 = createDataItem(index, ele);
                    $list.append($item1);
                });
                alert("添加成功");
                $(".turnHospitalAddInformation").css("display", "none");
                $(".turnHospitalInformationShow").css("display", "block");
            },
            error: function () {
                alert("添加失败");
            }
        });
    });
    //新增信息界面 返回按钮点击事件************************
    $("#turnHospitalAddBackBtn").click(function () {
        $(".turnHospitalAddInformation").css("display", "none");
        $(".turnHospitalInformationShow").css("display", "block");
    });
});